#include "common.h"

#define MAX_FPS 60

unsigned int iFrames = 0;
int iWidth, iHeight = 0;

float cameraX=0, cameraY=0, cameraZ = 0;
float camera_step = 1;

using namespace std;
GameLogic *game;

bool bFkeys[12];
char *messageQueue[6];
const int messageQueueSize = 5;
const int iSpeed = 500;

void drawText(int, int, char*);
void printMessageQueue()
{
	int y = 15;// |_
	glColor3f(1.0f, 1.0f, 1.0f);

	for (int i=0; i < messageQueueSize; i++)
	{
		if (messageQueue[i] != NULL)
		{
			drawText(10, iHeight-y, messageQueue[i]);
			y+= 15;
		}
	}
}

void addToMessageQueue(char *msg)
{
	for (int i=messageQueueSize; i > 0; i--)
	{
			messageQueue[i] = messageQueue[i-1];
	}
	messageQueue[0] = msg;
	// 5 lines max
}

void print(char *message)
{
	addToMessageQueue(message);
}

float getFPS()
{
	static unsigned int iLast	= 0;
	unsigned int		iNow	= 0;
	static float		fReturn = 0.0f;

	iNow = glutGet(GLUT_ELAPSED_TIME);



	if (iNow - iLast >= 500)
	{
		fReturn = (float)(iFrames*1000/((float)iNow - (float)iLast));
		iFrames = 0;
		iLast = iNow;
	}
	return fReturn;
}
void setOrthographicProjection()
{
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0, iWidth, 0, iHeight);
	glScalef(1, -1, 1);		// |-
	glTranslatef(0, (GLfloat)-iHeight, 0);
	glMatrixMode(GL_MODELVIEW);

}
void drawStatusMessage(char *message)
{
	
}
void drawText(int x, int y, char *text)
{
	glRasterPos2i(x,y);
	for (char *c=text;*c!= '\0';c++)
	{
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *c);
	}
}

void drawFPS()
{
	float fFPS = getFPS();
	char cFPS[20];
	//FPS: 3000.33
	sprintf_s(cFPS, "FPS: %.0f", fFPS);

	glColor3f(0.0f, 1.0f, 0.0f);
	drawText(10,20,cFPS);

}
void resetPerspectiveProjection()
{
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
}

void draw2DGrid()
{
	static int gridSize = 10;
	glColor3f(0.1f, 0.1f, 0.1f);
	glLineWidth(1);
	glBegin(GL_LINES);

	for (int x=0; x < iWidth; x+=gridSize)
	{
		glVertex2d(x, 0);
		glVertex2d(x, iHeight);
	}
	for (int y=0; y < iHeight; y += gridSize)
	{
		glVertex2d(0, y);
		glVertex2d(iWidth, y);
	}
	glEnd();
}
void draw2D()
{
	glPushMatrix();
	glLoadIdentity();
	setOrthographicProjection();

	// draw 2D now
	
	// F2: Draw 2D grid
	if (bFkeys[1])
		draw2DGrid(); // debug: draw grid

	// F1: Draw FPS
	if (bFkeys[0])
		drawFPS();

	printMessageQueue();

	// End of drawing
	glPopMatrix();
	resetPerspectiveProjection();
}
void draw3D()
{
	static float angle = 0.0f;
	glPushMatrix();
	
	if (angle > 360) angle = 0.0f;

	glRotatef(angle, 0.3f, 1.0f, 0.1f);
/*	glRotatef(angle, 0.0, 1.0, 0.0);
	glColor4f(0.9f, 0.9f, 0.9f, 0.5f);
	glBegin(GL_TRIANGLES);
		glVertex3f(-0.5f, -0.5f, 0.0f);
		glVertex3f(0.5f, 0.0f, 0.0f);
		glVertex3f(0.0f, 0.5f, 0.0f);
	glEnd();
*/
	glBegin(GL_QUADS);
		game->draw();

	glEnd();

	glPopMatrix();

	angle += 0.5f;

}
void renderScene()
{
	iFrames++;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	draw2D();
	draw3D();


	glutSwapBuffers();
}

void resize(int w, int h)
{
	h = (h==0)?1:h;

	iWidth = w;
	iHeight = (h==0)?1:h;

	float ratio = (float)w/h;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glViewport(0, 0, w, h);
	gluPerspective(45,ratio,1,1000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	gluLookAt(0.0, 0.0, 10.0,
				0.0, 0.0, -1.0,
				0.0, 1.0, 0.0);

}

void fkeys(int key, int x, int y)
{
	if (key <= 12) // f keys
		bFkeys[key-1] = !bFkeys[key-1];

	if (key == 5)
		print("PRessing ze evil F5 button!");
	if (key == 6)
		print("Pressing F6 SIX OH YES");

	switch (key)
	{
	case GLUT_KEY_LEFT:
		print("links");
		cameraX -= camera_step;
		break;

	case GLUT_KEY_RIGHT:
		print("rechts");
		cameraX += camera_step;
		break;

	case GLUT_KEY_UP:
		print("zoomin");
		cameraZ += camera_step;
		break;

	case GLUT_KEY_DOWN:
		print("zoomout");
		cameraZ -= camera_step;
		break;

	}
}
void debugMenuEvent(int wut)
{
	bFkeys[wut] = !bFkeys[wut];
}
void createMenus()
{
	int menu;

	menu = glutCreateMenu(debugMenuEvent);

	glutAddMenuEntry("Show fps", 0);
	glutAddMenuEntry("Show 2d grid", 1);
	glutAddMenuEntry("Limit fps", 2);

	glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void connecttest()
{

}

void gametick(int value)
{
//	print("gametick()");
//	game->tick();
//	glutTimerFunc(iSpeed,gametick,0);
}

void keyInput(int key, int x, int y)
{

}
void main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH|GLUT_RGBA|GLUT_DOUBLE);
	glutInitWindowSize(640,480);
	glutCreateWindow("Tetris!");
	//glutIgnoreKeyRepeat(1);

	//glClearColor(1.0, 1.0, 1.0, 1.0);

	glutDisplayFunc(renderScene);
	glutIdleFunc(renderScene); // get max fps!
	glutReshapeFunc(resize);
	glutSpecialFunc(fkeys);
//	glutKeyboardFunc(keyInput);
	memset(messageQueue, 0, sizeof(messageQueue));

	createMenus();

/*	TetrisNetwork *net = new TetrisNetwork();

	net->connectz();
*/
	glEnable(GL_DEPTH_TEST);

	game = new GameLogic();
	gametick(0);

	glutMainLoop();
}